@echo off
@rem This is where the project-specific tests will go

setlocal

set exe=python_d
set BZR_ERROR=0
set previous=%CD%
set bzr=
set pull=
set tool=%~pd0
for /F "Tokens=*" %%I in ('cygpath -u %tool%') do set tool_c=%%I

PATH %CD%\PCbuild;%PATH%

:CheckOpts
if "%1"=="stable" (set bzr=bzr-1.6rc1)	 	  & shift & goto CheckOpts
if "%1"=="dev" (set bzr=bzr.dev) & (set pull=y)   & shift & goto CheckOpts

:SetupEnv
if not defined APPDATA (set APPDATA=%tool%%bzr%-home)

@echo "Setting APPDATA to %APPDATA%"
@if not exist "%APPDATA%" md "%APPDATA%"

if not defined bzr goto Error
if not defined pull goto RunTests
if defined pull goto Pull

:Error
@echo "ERROR: First argument must be one of (stable, dev)"
@set BZR_ERROR=1
@goto End

:Rsync
rsync -Cavz --delete-excluded --exclude-from=%tool%\rsync-exclude bazaar-vcs.org::bazaar-ng/bzr/%bzr% %tool_c%
@copy %tool%%bzr%\.rsyncexclude %tool%\rsync-exclude

:Pull
@echo "Pulling latest changes from upstream into %tool%%bzr%"
@echo on
if not exist %tool%%bzr% goto Rsync
cd %tool%%bzr%
%exe% bzr pull
@echo off

:RunTests
@echo on

@echo "Changing to %tool%%bzr% directory"
cd %tool%%bzr%

@echo "Running tests for bzr version:"
@%exe% bzr --version

@echo "Running self tests"
%exe% bzr selftest -v
@if %ERRORLEVEL% NEQ 0 (set BZR_ERROR=%ERRORLEVEL%)

@echo "Running benchmark -- DISABLED"
@rem %exe% bzr selftest -v --benchmark
@rem if %ERRORLEVEL% NEQ 0 (set BZR_ERROR=%ERRORLEVEL%)

@echo "Removing temporary test directories"
@%exe% %tool%\rmtree.py %tool%%bzr%\test*.tmp

@echo off
cd %previous%

:End
exit /b %BZR_ERROR%
